layui.use(['form', 'jquery', 'layer', 'table', 'laydate','upload'], function() {
	var form = layui.form;
	var $ = layui.$;
	var table = layui.table;
	var layer = layui.layer;
	var laydate = layui.laydate;
	var upload = layui.upload;
	// 上传
	upload.render({
		elem:'#advent_picture_upload'+advent_id,
		url:upload_uri,
		accept:'images',
		acceptMime:'image/*',
		auto:true,
		field:'advent_picture_upload'+advent_id,
		multiple:false,
		drag:true,
		before:function(obj){
			layer.load();
		},
		done:function(res,index,upload){
			if(res.code == 0){
				var item = this.item
				var html = createUploadHtml('advent_picture',res.data);
				if(item == undefined){
					item = $('.layui-upload-drag');
				}
				item.prev('.layui-upload-list').html(html);
				layer.msg('上传成功',{icon:1});
			} else {
				layer.msg(e.msg,{icon:2});
			}
			layer.closeAll('loading'); 
		},
		error:function(index,upload){
			layer.msg('网络错误',{icon:2});
			layer.closeAll('loading');
		}
	});
	
	var lay_filter = 'tpl_advent' + advent_id;
	form.val(lay_filter, advent_form_value);
	// 初始化图片
	if(advent_form_value.advent_picture_info){
		var html = createUploadHtml('advent_picture',advent_form_value.advent_picture_info);
		$('#layui-upload-list'+advent_id).html(html);
	}

	// 监听提交
	form.on('submit(submit_advent)', function(data) {
		var field = data.field;
		var url = $('form[lay-filter="' + lay_filter + '"]').attr('action');
		if(field.advent_name == '') {
			layer.msg('广告名称必须填写', {
				icon: 2,
				amim: 6
			})
			$('input[name="advent_name"]').addClass('.layui-form-danger').focus();
			return false;
		}
		if(field.advent_url == '') {
			layer.msg('广告链接必须填写，不跳转请输入"#"', {
				icon: 2,
				amim: 6
			})
			$('input[name="advent_url"]').addClass('.layui-form-danger').focus();
			return false;
		}
		$.post(url, field, function(data) {
			if(data.code == 0) {
				layer.msg('保存成功', {
					icon: 1
				});
				// 关闭当前页 刷新父页
				$('.layui-body .layui-tab[lay-filter="content"] li.layui-this .layui-tab-close').trigger('click');
				$('.layui-body .layui-tab[lay-filter="content"] li[lay-id="advent"]').trigger('click');
				// 父页面点击
				$('.layui-body .layui-tab[lay-filter="content"] li[lay-id="advent_list"]').trigger('click');
				// 表格重载
				table.reload('advent_list');
			} else {
				layer.msg(data.msg, {
					icon: 2,
					anim: 6
				});
			}
		}, 'json')
		return false;
	})

})